//
//  WelcomeViewController.swift
//  sinaWeiBo
//
//  Created by smartlei on 15/10/18.
//  Copyright © 2015年 smartlei. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {
    
    //加载View//loadView比viewDidLoad先执行
    override func loadView() {
        //将控制器视图设置为图片视图
        view = backImgView
        //添加控件
        setupUI()
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
       
        iconImgView.sd_setImageWithURL(UserAccountViewModel.sharedUserAccountViewModel.avatarURL)

    }
    //// 视图已经显示，通常可以动画／键盘处理
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        //更新头像动画:更新约束的时候必须更新已经存在的约束，不要额外的添加约束
        iconImgView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-view.bounds.height+200)
        }
        nameLable.alpha = 0
        //添加UIView弹力系数动画
        UIView.animateWithDuration(2.0,
            delay: 0,
            usingSpringWithDamping: 0.5,
            initialSpringVelocity: 10,
            options: [],
            animations: { () -> Void in
        
               //重新部署view上的控件
                self.view.layoutIfNeeded()
                  self.nameLable.alpha = 1
            }) { (_) -> Void in
              //动画执行结束后发出通知切控制器
                NSNotificationCenter.defaultCenter().postNotificationName("WBNSNotificationCenterChangeViewController", object: nil)
              
        }
        
    }
    //添加背景图片
    lazy var  backImgView: UIImageView = UIImageView(name: "ad_background")
    //添加头像UIImageView
    lazy var  iconImgView: UIImageView = {
       
        //使用占位图占位
        let imgView = UIImageView(name: "ad_background")
        //设置圆角
        imgView.layer.cornerRadius = 45
        imgView.layer.masksToBounds = true
        
        return imgView
    }()
    //添加标签
    lazy var nameLable: UILabel = UILabel(labText: "欢迎归来", labTextColor: UIColor.blackColor(), fontSize: 17)
}

extension WelcomeViewController {
    
    //添加控件元素及自动布局
    private func setupUI() {
        
        //1 添加
        view.addSubview(iconImgView)
        view.addSubview(nameLable)
        
        //2 添加自动布局约束
        iconImgView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.width.equalTo(90)
            make.height.equalTo(90)
            make.bottom.equalTo(view.snp_bottom).offset(-200)
          
        }
        nameLable.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImgView)
            make.left.equalTo(iconImgView.snp_left)
            make.right.equalTo(iconImgView.snp_right)
            make.top.equalTo(iconImgView.snp_bottom).offset(20)
        }
    }
    
    
}
